package com.lw.sbdemo2.common.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

/**
 * Spring 启动初始化监听
 * 优势：可以注入bean操作；在web.xml无需额外配置
 * 执行顺序：早于过滤器和servlet的执行
 *
 * @author LinWang
 * @createTime 2016-12-29
 */
@Slf4j
@Component
public class SpringInitListener implements ApplicationListener<ContextRefreshedEvent> {

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {

        if (event.getSource() instanceof AnnotationConfigServletWebServerApplicationContext) {

            if (log.isInfoEnabled()) {
                log.info("【SpringInitListener】Spring启动初始化监听~");
            }

            //解决此类中操作SpringContextHolder的问题
            /*ApplicationContext context = event.getApplicationContext();
            SpringContextHolder.setApplicationContextStatic(context);*/

            //执行主业务逻辑
            doBiz();
        }
    }

    /**
     * 执行业务逻辑
     */
    private void doBiz() {
    }
}
